#include "core_semaphore.h"

CoreSemaphore::CoreSemaphore():waitcount(0), wakeupcount(0)
{

}


CoreSemaphore::~CoreSemaphore()
{

}

void CoreSemaphore::wait()
{
	std::unique_lock<std::mutex> lock(mymutex);
	if (-- this->waitcount < 0)
	{
		this->condition.wait(lock, [this]()->bool {
			return this->wakeupcount > 0;
			});
	}
	-- this->wakeupcount;
}

void CoreSemaphore::wakeup()
{
	std::lock_guard<std::mutex> lock(mymutex);
	if (++waitcount <= 0)
	{
		this->wakeupcount++;
		this->condition.notify_one();
	}
}
